package com.huang.util;

import cn.hutool.core.util.StrUtil;
import com.huang.constant.Common;
import com.huang.enums.UserCodeEnum;
import com.huang.exception.BizException;
import com.huang.resp.CommonResult;
import com.huang.vo.LoginUserInfo;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

/**
 * @author han
 * @since 2022-01-18 22:26:34
 */
public class OauthUtils {

    /**
     * 获取当前请求登录用户的详细信息
     */
    public static LoginUserInfo getCurrentUser() {
        String header = RequestContextUtils.getRequest().getHeader(Common.Token.JWT_PAYLOAD_KEY);
        if (StrUtil.isBlank(header)) {
            throw new BizException(CommonResult.error(UserCodeEnum.NOT_LOGIN));
        }
        try {
            return GsonUtils.stringToBean(URLDecoder.decode(header, "UTF-8"), LoginUserInfo.class);
        } catch (UnsupportedEncodingException e) {
            throw new BizException(CommonResult.error(UserCodeEnum.NOT_LOGIN));
        }
    }
}
